package com.ztesoft.web.checkup.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ztesoft.core.idproduce.ISequenceGenerator;
import com.ztesoft.framework.exception.BaseAppException;
import com.ztesoft.framework.log.ZTEsoftLogManager;
import com.ztesoft.framework.util.DateUtils;
import com.ztesoft.web.baseconfig.db.po.DmSrcTablePO;
import com.ztesoft.web.baseconfig.db.po.DmStrategyPO;
import com.ztesoft.web.checkup.service.CheckUpTaskService;
import com.ztesoft.web.common.DMSConstant;
import com.ztesoft.web.common.customdb.dao.DmTaskStageCustomDao;
import com.ztesoft.web.common.customdb.dao.dto.DmTaskStageDto;
import com.ztesoft.web.common.db.dao.DmDao;
import com.ztesoft.web.common.db.po.DmPO;

/**
 * 体检处理
 * 
 * @author lin.weibin@zte.com.cn
 * @date 2015年8月11日
 */
@Service("CheckUpTaskServiceImpl")
public class CheckUpTaskServiceImpl implements CheckUpTaskService {
	
	private static final ZTEsoftLogManager LOGGER = ZTEsoftLogManager.getLogger(CheckUpTaskServiceImpl.class);
	
	@Resource(name="sequenceProcGenerator")
	private ISequenceGenerator sequenceGenerator;
	
	@Autowired
	private DmDao dmDao;
	
	@Autowired
	private DmTaskStageCustomDao taskStageCustomDao;
	
	@Override
	public int addCheckUp(List<Map<String, Object>> list, String state, DmStrategyPO strategyPO, 
			int taskId, DmSrcTablePO dmSrcTablePO, DmTaskStageDto taskStagePO) throws Exception{
		List<DmPO> resultList = new ArrayList<DmPO>(list.size());
		StringBuilder sb;
		DmPO result;
		Date now = DateUtils.getDBCurrentTime();
		Object obj = null;
		int idloop = 0;
		Long[] ids = getIds();
		for (int i = 0; i < list.size(); i++) {
			now = DateUtils.getDBCurrentTime();
			sb = new StringBuilder();
			result = new DmPO();
			Iterator<Entry<String, Object>> lte = list.get(i).entrySet().iterator();
			while (lte.hasNext()) {
				obj = lte.next().getValue();
				if (obj instanceof java.util.Date) {//时间作转换2015-08-27 15:33:09.0
					sb.append(DateUtils.date2String((Date)obj)).append(",");
				} else {//
					sb.append(obj).append(",");
				}
			}
			result.setDmDetailId(ids[idloop]);			
			idloop = idloop + 1;
			if (idloop >= DMSConstant.TABLE_IDS) {//超过重新获取
				ids = getIds();
				idloop = 0;
			}
			result.setDmStrategyId(strategyPO.getDmStrategyId());
			result.setDmTaskId(taskId);
			result.setDmObjectName(taskStagePO.getCheckObjName());
			result.setDmObjectKeyName(dmSrcTablePO.getKeyColumn());
			result.setDmObjectKeyValue(sb.substring(0, sb.length() - 1));
			result.setRecordCount(1);
			result.setCreateDate(now);
			result.setState(state);
			result.setStateDate(now);
			resultList.add(result);
		}
		LOGGER.debug("批量插入DM_"+taskStagePO.getCheckObjName()+","+resultList.size());
		dmDao.insertBatch("DM_"+taskStagePO.getCheckObjName(),resultList);
		taskStagePO.setCheckCount(resultList.size());
		taskStagePO.setStateDate(now);
		taskStagePO.setCheckDataSize(BigDecimal.valueOf(((double)dmSrcTablePO.getOneRecordSize()*(taskStagePO.getCheckCount()))/1024)
				.setScale(2, RoundingMode.HALF_UP));
		LOGGER.debug("更新任务环节表"+taskStagePO.getCheckObjName()+","+resultList.size());
		taskStageCustomDao.updateTaskStageCheckCount(taskStagePO);
		return resultList.size();
	}

	/**
	 * 获取主键ID
	 * @return
	 * @throws BaseAppException 
	 */
	private Long[] getIds() throws BaseAppException {
		return sequenceGenerator.sequenceBatchLongValue("DM","DM_DETAIL_ID", DMSConstant.TABLE_IDS);
	}
	
}
